import java.awt.*;
import java.awt.event.*;

public class TextTool implements KeyboardTool {
   
  protected DrawingCanvas canvas;
  protected Point startingPosition;
  protected StringBuffer text;
  protected Font font = new Font("Serif", Font.BOLD, 24);
// protected FontMetrics fm =
//           Toolkit.getDefaultToolkit().getFontMetrics(font); // deprecated method
//  protected int ascent = fm.getAscent();  //not needed here
//  protected int descent = fm.getDescent();  //not needed here

  public TextTool(DrawingCanvas canvas) {
   this.canvas = canvas;
  }
  TextObject mytext;
 public void mousePressed(MouseEvent e)  {
   /* Establish Focus and starting position for text display */
   canvas.requestFocus();
   startingPosition = e.getPoint();
   Graphics iBGraphics = canvas.getImageBufferGraphics();
   iBGraphics.setFont(font);
   text = new StringBuffer();
   mytext = new TextObject(startingPosition,canvas.getImageBufferGraphics().getColor(), font);
   IDrawingObjectLink mytextLink=new IDrawingObjectLink(mytext);
   mytextLink.next=canvas.firstObject;
   canvas.firstObject=mytextLink;
 }

  public void keyPressed(KeyEvent e)  {
   /* add character to string buffer */
   char nextChar = e.getKeyChar(); 
   text.append(nextChar);
   mytext.setText(text.toString());
   Graphics iBGraphics = canvas.getImageBufferGraphics();
   iBGraphics.drawString(text.toString(), startingPosition.x,startingPosition.y); 
   canvas.repaint();
  }


   public void mouseReleased(MouseEvent e) { } 
   public void mouseDragged(MouseEvent e) { }
   public void keyReleased(KeyEvent e) { }
   public void keyTyped(KeyEvent e) { }
}
